"""
Created on 2018/4/19 14:12 星期四
@author: Matt  zhuhan1401@126.com
Description: 使用scikit-learn完成手写识别程序 demo
"""

from sklearn import datasets
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.externals import joblib
from matplotlib import pyplot as plt

def display(digits):
    imagesAndLabels = list(zip(digits.images, digits.target))
    plt.figure(figsize=(8, 6), dpi=200)
    for index, (image, label) in enumerate(imagesAndLabels[:8]):
        plt.subplot(2, 4, index + 1)
        plt.axis('off')
        plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
        plt.title('Digit: %i' % label, fontsize=20)
    plt.show()

def saveModelPara(clf):
    joblib.dump(clf,'digits_SVM.pkl')

digits = datasets.load_digits()  # 导入数据
# display(digits)
XTrain,XTest,YTrain,YTest=train_test_split(digits.data,digits.target,test_size=0.20,random_state=2)# 打乱顺序
clf=svm.SVC(gamma=0.001,C=100.)
clf.fit(XTrain,YTrain)
print(clf.score(XTest,YTest))
# saveModelPara(clf)


